<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="page" uri="http://cch.hn/page/" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>客户管理系统</title>

    <!-- Bootstrap Core CSS -->
    <link href="<%=basePath%>css/bootstrap.min.css" rel="stylesheet">

    <!-- MetisMenu CSS -->
    <link href="<%=basePath%>css/metisMenu.min.css" rel="stylesheet">

    <!-- DataTables CSS -->
    <link href="<%=basePath%>css/dataTables.bootstrap.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link href="<%=basePath%>css/sb-admin-2.css" rel="stylesheet">

    <!-- Custom Fonts -->
    <link href="<%=basePath%>css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="<%=basePath%>css/boot-crm.css" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!-- [if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <[endif] -->

</head>

<body>

<div id="wrapper">

    <div id="page-wrapper">
        <div class="row">
            <div class="col-lg-12">
                <h2>客户信息管理系统</h2>
            </div>
            <!-- /.col-lg-12 -->
        </div>
        <!-- /.row -->
        <div class="panel panel-default">
            <div class="panel-body">
                <form class="form-inline" action="${pageContext.request.contextPath}/customer/customers.action"
                      method="get">
                    <div class="form-group">
                        <label for="customer_name" style="float:left;padding:7px 21px 0 28px;">客户名称</label>
                        <input type="text" class="form-control" id="customer_name" value="${name}" name="name">
                    </div>
                    <div class="form-group">
                        <label for="customer_level" style="float:left;padding:7px 21px 0 63px;">客户来源</label>
                        <select class="form-control" id="customer_level" name="level.id">
                            <option value="0">--请选择--</option>
                            <c:forEach items="${levels}" var="l">
                                <option value="${l.id}" <c:if
                                        test="${l.id == level.id}">selected</c:if>>${l.name }</option>
                            </c:forEach>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="customer_source" style="float:left;padding:7px 21px 0 28px;">客户来源</label>
                        <select class="form-control" id="customer_source" placeholder="客户来源" name="source.id">
                            <option value="0">--请选择--</option>
                            <c:forEach items="${sources}" var="s">
                                <option value="${s.id}"
                                        <c:if test="${s.id == source.id}">selected</c:if>>${s.name }</option>
                            </c:forEach>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="customer_industry" style="float:left;padding:7px 21px 0 28px;">客户来源</label>
                        <select class="form-control" id="customer_industry" name="industry.id">
                            <option value="0">--请选择--</option>
                            <c:forEach items="${industries}" var="i">
                                <option value="${i.id}"
                                        <c:if test="${i.id == industry.id}">selected</c:if>>${i.name }</option>
                            </c:forEach>
                        </select>
                    </div>
                    <div class="form-group" style="float:right;">
                        <button type="submit" class="btn btn-primary" >查询</button>
                        <label for="customer_source" style="padding:7px 28px 0 21px;"></label>
                    </div>

                </form>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-heading" align="center"><strong>客户信息分页列表</strong></div>
                    <!-- /.panel-heading -->
                    <table class="table table-bordered table-striped">
                        <thead>
                        <tr>
                            <th>客户编号</th>
                            <th>客户名称</th>
                            <th>客户电话</th>
                            <th>客户地址</th>

                            <th>客户人员</th>

                            <th>客户级别</th>
                            <th>客户来源</th>
                            <th>客户行业</th>

                            <th>客户操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <c:forEach items="${page.data}" var="customer">
                            <tr>
                                <th>${customer.id}</th>
                                <th>${customer.name}</th>
                                <th>${customer.phone}</th>
                                <th>${customer.address}</th>

                                <th>${customer.user.username}</th>

                                <th>${customer.level.name}</th>
                                <th>${customer.source.name}</th>
                                <th>${customer.industry.name}</th>
                                <th>
                                    <a href="#" class="btn btn-info btn-xs" data-toggle="modal"
                                       data-target="#customeOperateDialog"
                                       onclick="selectCustomer(${customer.id})">客户详情</a>
                                </th>
                            </tr>
                        </c:forEach>
                        </tbody>
                    </table>
                    <div class="col-md-12 text-right">
                        <page:page url="${pageContext.request.contextPath }/customer/customers.action"/>
                    </div>
                    <!-- /.panel-body -->
                </div>
                <!-- /.panel -->
            </div>
            <!-- /.col-lg-12 -->
        </div>
    </div>
    <!-- /#page-wrapper -->
</div>


<!-- 客户操作对话框 -->
<div class="modal fade" id="customeOperateDialog" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel"><strong>客户信息</strong></h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" id="operate_customer_form">
                    <input type="hidden" id="operate_customer_id" name="id"/>
                    <div class="form-group">
                        <label for="operate_customer_name" style="float:left;padding:7px 15px 0 27px;">客户名称</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="operate_customer_name" placeholder="客户名称"
                                   name="name">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="operate_customer_phone" style="float:left;padding:7px 15px 0 27px;">客户电话</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="operate_customer_phone" placeholder="客户电话"
                                   name="phone">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="operate_customer_address" style="float:left;padding:7px 15px 0 27px;">客户地址</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="operate_customer_address" placeholder="客户地址"
                                   name="address">
                        </div>
                    </div>


                    <div class="form-group">
                        <label for="operate_customer_user_id" style="float:left;padding:7px 15px 0 27px;">客户人员</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="operate_customer_user_id" placeholder="客户联系人"
                                    name="user.id">
                                <c:forEach items="${users}" var="u">
                                    <option value="${u.id}"
                                            <c:if test="${u.id == operate_customer_user_id}">selected</c:if>>${u.username }</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="operate_customer_level_id" style="float:left;padding:7px 15px 0 27px;">客户级别</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="operate_customer_level_id" placeholder="客户级别"
                                    name="level.id">
                                <c:forEach items="${levels}" var="l">
                                    <option value="${l.id}"
                                            <c:if test="${l.id == operate_customer_level_id}">selected</c:if>>${l.name }</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="operate_customer_source_id" style="float:left;padding:7px 15px 0 27px;">客户来源</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="operate_customer_source_id" placeholder="客户来源"
                                    name="source.id">
                                <c:forEach items="${sources}" var="s">
                                    <option value="${s.id}"
                                            <c:if test="${s.id == operate_customer_source_id}">selected</c:if>>${s.name }</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="operate_customer_industry_id"
                               style="float:left;padding:7px 15px 0 27px;">客户行业</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="operate_customer_industry_id" placeholder="客户行业"
                                    name="industry.id">
                                <c:forEach items="${industries}" var="i">
                                    <option value="${i.id}"
                                            <c:if test="${i.id == operate_customer_industry_id}"> selected</c:if>>${i.name }</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-success" onclick="insertCustomer()">添加</button>
                <button type="button" class="btn btn-warning" onclick="updateCustomer()">修改</button>
                <button type="button" class="btn btn-danger" onclick="deleteCustomer()">删除</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>


<!-- jQuery -->
<script src="<%=basePath%>js/jquery.min.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="<%=basePath%>js/bootstrap.min.js"></script>

<!-- Metis Menu Plugin JavaScript -->
<script src="<%=basePath%>js/metisMenu.min.js"></script>

<!-- DataTables JavaScript -->
<script src="<%=basePath%>js/jquery.dataTables.min.js"></script>
<script src="<%=basePath%>js/dataTables.bootstrap.min.js"></script>

<!-- Custom Theme JavaScript -->
<script src="<%=basePath%>js/sb-admin-2.js"></script>

<script type="text/javascript">
    function selectCustomer(id) {
        $.ajax({
            type: "get",
            url: "<%=basePath%>customer/customer.action",
            data: {"id": id},
            success: function (c) {
                $("#operate_customer_id").val(c.id);
                $("#operate_customer_name").val(c.name);
                $("#operate_customer_phone").val(c.phone);
                $("#operate_customer_address").val(c.address);
                $("#operate_customer_user_id").val(c.user.id);
                $("#operate_customer_level_id").val(c.level.id);
                $("#operate_customer_source_id").val(c.source.id);
                $("#operate_customer_industry_id").val(c.industry.id);
            }
        });
    }


    function insertCustomer() {
        if (confirm('是否要添加该客户？')) {
            $.post("<%=basePath%>customer/insert.action", $("#operate_customer_form").serialize(), function (data) {
                if (data.status) {
                    alert("客户[" + data.name + "]信息添加成功！");
                    window.location.reload();
                } else {
                    alert("客户[" + data.name + "]信息添加失败！");
                }
            });
        }
    }


    function updateCustomer() {
        if (confirm('是否要修改该客户？')) {
            $.post("<%=basePath%>customer/update.action", $("#operate_customer_form").serialize(), function (data) {
                if (data.status) {
                    alert("客户[" + data.name + "]信息修改成功！");
                    window.location.reload();
                } else {
                    alert("客户[" + data.name + "]信息修改失败！");
                }
            });
        }
    }

    function deleteCustomer() {
        if (confirm('是否要删除该客户？')) {
            $.post("<%=basePath%>customer/delete.action", $("#operate_customer_form").serialize(), function (data) {
                if (data.status) {
                    alert("客户[" + data.name + "]信息删除成功！");
                    window.location.reload();
                } else {
                    alert("客户[" + data.name + "]信息删除失败！");
                }
            });
        }
    }
</script>


</body>

</html>
